<template>
  <el-row>
    <el-col :span="12">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px">
        <el-form-item label="原密码" prop="oldPass">
          <el-input type="password" v-model="ruleForm.oldPass" autocomplete="off" show-password></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="pass">
          <el-input type="password" v-model="ruleForm.pass" autocomplete="off" show-password></el-input>
        </el-form-item>
        <el-form-item label="确认新密码" prop="checkPass">
          <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off" show-password></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm(ruleForm)">提交修改</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<script>
  import {updatedPwd} from '@/api/oneseIf-centre'

  /**
   *{ 个人信息更新密码 }
   * @param:
   * @author: 宋康
   * @DateTime: 2022/7/1
   */
  export default {
    name: "PersonalUpdatePwd",
    data() {
      const validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      const validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.pass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        ruleForm: {
          oldPass: '',
          pass: '',
          checkPass: ''
        },
        rules: {
          oldPass: [
            {required: true, message: '请输入原密码', trigger: 'blur'}
          ],
          pass: [
            {validator: validatePass, trigger: 'blur'},
            {required: true, message: '请输入新密码', trigger: 'blur'},
            {min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur'}
          ],
          checkPass: [
            {validator: validatePass2, trigger: 'blur'},
            {required: true, message: '请输入确认新密码', trigger: 'blur'},
            {min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur'}
          ],
        }
      }
    },
    methods: {
      /**
       * 修改密码
       * @param ruleForm
       */
      submitForm(ruleForm) {
        updatedPwd(ruleForm.oldPass, ruleForm.pass, ruleForm.checkPass).then(res => {
          if (res.status) {
            this.$XModal.alert({
              content: '修改成功',
              status: 'success',
            });
          } else {
            this.$XModal.alert({
              content: res['msg'],
              status: 'error',
            })
          }
        })
      },
    }
  }
</script>

<style scoped>

</style>
